package com.viphuli.app.tool.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import butterknife.Bind;

import com.viphuli.app.tool.R;
import com.viphuli.app.tool.activity.MyToolBarActivity;
import com.viphuli.app.tool.common.JavaScript;

public class SingleWebViewFragment extends BaseProgressFragment {
	public static final String BROWSER_URL = "browser_url";

	@Bind(R.id.id_single_webview)
	protected WebView mWebView;

	private String mCurrentUrl;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_webview;
	}

	@Override
	protected void initParams() {

		Bundle bundle = getActivity().getIntent().getBundleExtra(MyToolBarActivity.BUNDLE_KEY_ARGS);

		if (bundle != null) {
			if (bundle.containsKey(BROWSER_URL))
				mCurrentUrl = bundle.getString(BROWSER_URL);
		}

	}

	@Override
	protected void initView() {

		// 取消硬件加速
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		}

		mWebView.getSettings().setJavaScriptEnabled(true);//支持js
		mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		mWebView.getSettings().setBuiltInZoomControls(false);
		mWebView.getSettings().setLoadWithOverviewMode(true);
		mWebView.getSettings().setUseWideViewPort(false); // 将图片调整到适合webview的大小 
		mWebView.getSettings().setBlockNetworkImage(false);// 把图片加载放在最后来加载渲染
		mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);// 提高渲染的优先级
		mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//关闭webview中缓存 
		mWebView.getSettings().setNeedInitialFocus(true); //当webview调用requestFocus时为webview设置节点
		mWebView.getSettings().setLoadsImagesAutomatically(true); //支持自动加载图片

		//映射Java对象到一个名为”xs“的Javascript对象上
		//JavaScript中可以通过"window.viphuli"来调用Java对象的方法
		mWebView.addJavascriptInterface(JavaScript.newInstance(getActivity()), JavaScript.NAME);

		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				//打加载对话框
				showLoadDialog();
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				//关闭加载对话框 
				hideLoadDialog(true);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);//点击连接从自身打开
				return true;
			}

		});

		//屏蔽长按
		mWebView.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View arg0) {
				return true;
			}
		});

		//文件下载
		mWebView.setDownloadListener(new DownloadListener() {

			@Override
			public void onDownloadStart(String arg0, String arg1, String arg2, String arg3, long arg4) {
				Uri uri = Uri.parse(arg0);
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				getActivity().startActivity(intent);
			}
		});

	}

	@Override
	protected void initData() {

		mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		mWebView.loadUrl(mCurrentUrl);

	}

}
